﻿using Application.Common.Interface;
using AutoMapper;
using Core.Extensions;
using Core.Util;
using Domain.Dto.SiteMap;
using Domain.Model;
using Microsoft.AspNetCore.Mvc;

namespace Web.Controllers
{
    public class SiteMapController : Controller
    {
        private readonly ILogger<SiteMapController> logger;
        private readonly IMapper mapper;
        private readonly ISiteMapRepository siteMapRepository;
        private readonly ISelectorRepository selectorRepository;
        private readonly IWebScraperRepository webScraperRepository;
        public SiteMapController(ISiteMapRepository siteMapRepository, IMapper mapper, IWebScraperRepository webScraperRepository, ISelectorRepository selectorRepository,IServiceProvider provider)
        {
            this.siteMapRepository = siteMapRepository;
            this.mapper = mapper;
            this.webScraperRepository = webScraperRepository;
            this.selectorRepository = selectorRepository;
        }

        public async Task<IActionResult> Scraper(int Id)
        {
            var siteMap = await siteMapRepository.GetAsync(Id);
            if (siteMap is null)
                throw new NullReferenceException(nameof(siteMap));
            if (siteMap.isscraper)
            {
                return RedirectToAction(nameof(Browse), new { Id });
            }
            siteMap.isscraper = true;
            await siteMapRepository.UpdateAsync(siteMap);
            var selRoot = await selectorRepository.GetBySiteMapIdAsync(Id);
            if (selRoot is null)
                throw new NullReferenceException(nameof(selRoot));
            var tree = await selectorRepository.GetTreeSelectorByIdAsync(selRoot.Id);
            Task.Run(()=>webScraperRepository.Scraper(siteMap,tree));
            return RedirectToAction(nameof(Browse), new {Id});
        }
        public async Task<IActionResult> Browse(int Id)
        {
            var siteMap = await siteMapRepository.GetAsync(Id);
            ViewBag.isscraper = siteMap.isscraper;
            var table = ExcelHelper.GetTableData($"{siteMap.Name}.xlsx");
            return View(table);
        }
        public async Task<IActionResult> Add()
        {
            return View();
        }
        [HttpPost]

        public async Task<IActionResult> Add(SiteMapDto dto)
        {
            var model = mapper.Map<SiteMap>(dto);
            model.Domain = dto.Url.UrlGetDomain();
            await siteMapRepository.AddAsync(model);
            return RedirectToAction(nameof(Index), "Home");
        }
        public async Task<IActionResult> Edit(int Id)
        {
            var model = await siteMapRepository.GetAsync(Id);
            var res = mapper.Map<SiteMapDto>(model);
            return View(res);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(SiteMapDto dto)
        {
            var add = mapper.Map<SiteMap>(dto);
            add.Domain = add.Url.UrlGetDomain();
            await siteMapRepository.UpdateAsync(add);
            return RedirectToAction(nameof(Index), "Home");
        }
        public async Task<IActionResult> Delete(int Id)
        {
            await siteMapRepository.DeleteAsync(Id);
            return RedirectToAction(nameof(Index), "Home");
        }
    }
}
